package chaoyue.study.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 线程池提交任务的两种方式
 * execute 处理Runnable类型的任务，没有返回值
 * submit 可以处理Runnable，也可以处理Callable，返回Future用来获取执行结果
 */
public class ThreadPoolTest3 {
    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        pool.execute(() -> System.out.println("task by submit..."));
        Future<Integer> submit1 = pool.submit(() -> 1);
        Future<String> submit2 = pool.submit(() -> "hello");
        Future<Integer> submit3 = pool.submit(() -> 1 / 0);
        System.out.println(submit1.get());
        System.out.println(submit2.get());
        System.out.println(submit3.exceptionNow());
        pool.shutdown();
    }
}
